<?php
/**
 * Created by PhpStorm.
 * User: ljc
 * Date: 2018/7/29
 * Time: 14:49
 */
namespace wechat;
class Wx
{
	static $config=[];
	protected $message;
	public function __construct (array $config=[]) {
		if(!empty($config)){
			self::$config=$config;
		}
		$this->message=$this->parseMessage ();
	}
	//验证绑定服务器
	public function valid(){
		if(isset($_GET['signature'])&&isset($_GET['timestamp'])&&isset($_GET['nonce'])&&isset($_GET['echostr'])){
			$signature=$_GET['signature'];
			$timestamp=$_GET['timestamp'];
			$nonce=$_GET['nonce'];
			$token=self::$config['token'];
			$tmpArr=array($timestamp,$nonce,$token);
			sort($tmpArr,SORT_STRING);
			$tmpstr=implode($tmpArr);
			$tmpstr=sha1($tmpstr);
			if($tmpstr==$signature){
				echo $_GET['echostr'];
			}
		}

	}
	//获取粉丝发送过来的消息
	public function parseMessage(){
		//获取服务器上数据流的数据
		$xml = file_get_contents('php://input');
		if($xml){
			$message = simplexml_load_string($xml,'simpleXMLElement',LIBXML_NOCDATA);
			//将接受到的数据返回出去
			return $message;
		}
	}
	//返回获取到的消息
	public function getMessage(){
		return $this->message;
	}
	public function instance($class){
		$class='\wechat\build\\'.ucfirst ($class);
		return new $class;
	}

	/**
	 * 建立服务器和微信服务器的通讯
	 * @param       $url
	 * @param array $field
	 *
	 * @return string
	 */
	public function curl($url, $field = [])
	{
		//初始化curl
		$ch = curl_init();
		//设置curl参数
		//设置请求地址
		curl_setopt($ch, CURLOPT_URL, $url);
		//设置接收返回结果,但不直接展示在界面
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		//设置禁止证书校验
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		//判断是否为post请求
		if (!empty($field)) {
			//定义超时时间
			curl_setopt($ch, CURLOPT_TIMEOUT, 30);
			//开启post请求
			curl_setopt($ch, CURLOPT_POST, 1);
			//传递post数据
			curl_setopt($ch, CURLOPT_POSTFIELDS, $field);
		}

		//发送请求,定义一个变量接收请求结果
		$data = '';
		//发送请求可能失败,所以我需要判断,如果请求成功,将请求结果赋值给$data,反之,不给$data赋值
		if (curl_exec($ch)) {
			$data = curl_multi_getcontent($ch);
		}
		//关闭curl
		curl_close($ch);
		//返回接收到的数据
		return $data;
	}

	/**
	 * 获取accessToken
	 * @return bool
	 */
	public function getAccessToken(){
		//定义缓存目录的地址
		$dir = __DIR__ . '/cache';
		is_dir($dir) || mkdir($dir);
		//定义文件名称
		$filename = $dir . '/token_' . md5(self::$config['appID']) . '.php';
		if (is_file($filename) && filemtime($filename) + 7100 > time()){
			$data = include $filename;
		}else{
			//设置请求的地址
			$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.self::$config['appID'].'&secret='.self::$config['appsecret'];
			//使用curl方法发送get请求,获取access_token
			$data = $this->curl($url);
			$data = json_decode($data,true);
			if (isset($data['errcode'])){
				return false;
			}
			file_put_contents($filename,"<?php return " . var_export($data,true) . ";\n?>");
		}
		//将access_token的值返回
		return $data['access_token'];
	}
	/**
	 * 获取jssdk票据
	 */
	public function getTicket(){
		//定义缓存目录的地址
		$dir = __DIR__ . '/cache';
		is_dir($dir) || mkdir($dir);
		//定义文件名称
		$filename = $dir . '/ticket_' . md5(self::$config['appID']) . '.php';
		if (is_file($filename) && filemtime($filename) + 7100 > time()){
			$data = include $filename;
		}else{
			$token=$this->getAccessToken ();
			//设置请求的地址
			$url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$token.'&type=jsapi';
			//使用curl方法发送get请求,获取access_token
			$data = $this->curl($url);
			var_dump ($data);
			$data = json_decode($data,true);
			if ($data['errcode']!=0){
				return false;
			}
			file_put_contents($filename,"<?php return " . var_export($data,true) . ";\n?>");
		}
		//将access_token的值返回
		return $data['ticket'];
	}
}